﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;

namespace KD.DataImport
{
    public class ImageHelper
    {
        /// <summary>
        /// 数组转图片
        /// </summary>
        /// <param name="bImage"></param>
        /// <returns></returns>
        public static Image ByteToImage(byte[] bImage)
        {
            if (bImage.Length == 0)
                return null;
            System.IO.MemoryStream ms = new System.IO.MemoryStream(bImage);
            System.Drawing.Image image = System.Drawing.Image.FromStream(ms);
            return image;
        }

        /// <summary>
        /// 图片转数据
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static byte[] ImageToByte(Image image)
        {
            MemoryStream ms = new MemoryStream();
            if (image == null)
                return new byte[ms.Length];
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] BPicture = new byte[ms.Length];
            BPicture = ms.GetBuffer();
            return BPicture;
        }

        /// <summary>
        /// 裁剪图片
        /// </summary>
        /// <param name="originImage">原图片</param>
        /// <param name="region">裁剪的方形区域</param>
        /// <returns>裁剪后图片</returns>
        public static Image CropImage(Image originImage, Rectangle region)
        {
            Bitmap result = new Bitmap(region.Width, region.Height);
            Graphics graphics = Graphics.FromImage(result);
            Rectangle destRect = new Rectangle(0, 0, region.Width, region.Height);//矩形容器 
            Rectangle srcRect = new Rectangle(region.X, region.Y, region.Width, region.Height);
            graphics.DrawImage(originImage, destRect, srcRect, GraphicsUnit.Pixel);
            return result;
        }

        /// <summary>
        /// 不管多大的图片都能在指定大小picturebox控件中显示
        /// </summary>
        /// <param name="bitmap">图片</param>
        /// <param name="destHeight">picturebox控件高</param>
        /// <param name="destWidth">picturebox控件宽</param>
        /// <returns></returns>
        public static Image ZoomImage(Image bitmap, int destHeight, int destWidth)
        {
            try
            {
                System.Drawing.Image sourImage = bitmap;
                int width = 0, height = 0;
                //按比例缩放             
                int sourWidth = sourImage.Width;
                int sourHeight = sourImage.Height;
                if (sourHeight > destHeight || sourWidth > destWidth)
                {
                    if ((sourWidth * destHeight) > (sourHeight * destWidth))
                    {
                        width = destWidth;
                        height = (destWidth * sourHeight) / sourWidth;
                    }
                    else
                    {
                        height = destHeight;
                        width = (sourWidth * destHeight) / sourHeight;
                    }
                }
                else
                {
                    width = sourWidth;
                    height = sourHeight;
                }
                Bitmap destBitmap = new Bitmap(destWidth, destHeight);
                Graphics g = Graphics.FromImage(destBitmap);
                g.Clear(Color.Transparent);
                //设置画布的描绘质量           
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(sourImage, new Rectangle((destWidth - width) / 2, (destHeight - height) / 2, width, height), 0, 0, sourImage.Width, sourImage.Height, GraphicsUnit.Pixel);
                //g.DrawImage(sourImage, new Rectangle(0, 0, destWidth, destHeight), new Rectangle(0, 0, sourImage.Width, sourImage.Height), GraphicsUnit.Pixel);
                g.Dispose();
                //设置压缩质量       
                System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
                long[] quality = new long[1];
                quality[0] = 100;
                System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                encoderParams.Param[0] = encoderParam;
                sourImage.Dispose();
                return destBitmap;
            }
            catch (Exception ex)
            {
                return bitmap;
            }
        }


        /// <summary>
        /// 将Image类型转换成Base64字符串
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static string DoGetBase64FromImage(Image image)
        {
            string strbaser64 = "";
            try
            {
                Bitmap bmp = new Bitmap(image);
                MemoryStream ms = new MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                strbaser64 = Convert.ToBase64String(arr);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.ToString());
            }
            return strbaser64;
        }


        /// <summary>
        /// 从文件读取图片
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Image DoGetImageFromFile(string path)
        {
            if (File.Exists(path))
            {
                FileStream fs = File.OpenRead(path);
                int fileLength = (int)fs.Length;
                byte[] image = new byte[fileLength];
                fs.Read(image, 0, fileLength);
                return Image.FromStream(fs);
            }
            else
                return null;
        }

        /// <summary>
        /// 通过字符串提取图片
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static Image DoGetImageFromBase64(string image)
        {
            //image = image.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//将base64头部信息替换
            byte[] bytes = Convert.FromBase64String(image);
            MemoryStream memStream = new MemoryStream(bytes);
            return Image.FromStream(memStream);
        }




    }
}
